using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentData;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using System.Data;

namespace AdverseEventReportSystem.Repository 
{
    public class AdverseEventReasonRecordRepository : BaseRepository<AdverseEventReasonRecord>
    {

        #region 构造函数

        private static AdverseEventReasonRecordRepository _instance;
        public static AdverseEventReasonRecordRepository GetInstance()
        {
            if (_instance == null)
            {
                _instance = new AdverseEventReasonRecordRepository();
            }
            return _instance;
        }

        #endregion

        #region Add

        public int Add(AdverseEventReasonRecord entity, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            int Id = context.Insert<AdverseEventReasonRecord>("AdverseEventReasonRecord", entity)
                    .AutoMap(x => x.ID)
                    .ExecuteReturnLastId<int>();
            return Id;
        }

        #endregion

        #region Update

        public int Update(AdverseEventReasonRecord entity, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            int rowsAffected = context.Update<AdverseEventReasonRecord>("AdverseEventReasonRecord", entity)
                    .AutoMap(x => x.ID)
                    .Where(x => x.ID)
                    .Execute();
            return rowsAffected;
        }

        #endregion

        #region 获取单个实体 通过主键ID

        /// <summary>
        /// 获取单个实体 通过主键ID
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual AdverseEventReasonRecord GetQuerySingleByID(int id, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = id;
            string sqlStr = "SELECT * FROM AdverseEventReasonRecord WHERE ID=@0";
            AdverseEventReasonRecord obj = GetQuerySingle(sqlStr, context, objects);
            return obj;
        }

        #endregion

        #region 删除操作  通过ID

        /// <summary>
        /// 删除操作  通过ID
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual int DelByID(int id, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = id;
            string sqlStr = "DELETE FROM AdverseEventReasonRecord WHERE ID=@0";
            return Del(sqlStr, context, objects);
        }

        #endregion

        #region 根据不良事件ID删除

        /// <summary>
        /// 根据事件ID删除
        /// </summary>
        /// <param name="eventID">不良事件ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual int DelByEventID(int eventID, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = eventID;
            string sqlStr = "DELETE FROM AdverseEventReasonRecord WHERE EventID=@0";
            return Del(sqlStr, context, objects);
        }

        #endregion

        #region 获取实体DataTable

        /// <summary>
        /// 根据事件ID和类型
        /// </summary>
        /// <param name="typeID">不良事件ID</param>
        /// <param name="typeID">事件类型ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual DataTable GetAdverseEventReasonRecordTable(int eventID,int typeID, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[2];
            objects[0] = typeID;
            objects[1] = eventID;
            string sqlStr = @"with tb as
                             (
                               select top 100 ID,ParentId,cast(Reason as varchar(max)) Reason,1 Level,Sort,cast(ID as varchar(max)) Path, cast(right('00000'+ltrim(row_number() over (order by Sort,Id)),5) as varchar(max))PathSort from AdverseEventReason where ParentId = 0 and TypeID = @0 order by Sort,Id
                               union all
                               select a.ID,a.ParentId,b.Reason+'#'+cast(a.Reason as varchar(max)) Reason,Level+1,a.Sort,b.Path +'#'+ cast(a.ID as varchar(max)) Path,PathSort + cast(right('00000'+ltrim(a.Sort),5) as varchar(max)) from AdverseEventReason a join tb b on a.parentid=b.Id
                             )
                             select a.ID AS ReasonID,a.ParentId,b.Reason,b.Level,b.Path,b.PathSort,c.ReasonID IsChecked,c.OtherReason,(select count(1) from AdverseEventReason where ParentId = a.Id) Cnt from AdverseEventReason a inner join tb b on a.ID = b.ID left join AdverseEventReasonRecord c on a.ID = c.ReasonID and c.EventID = @1
                             order by PathSort";

            return GetDataTable(sqlStr, context, objects);
        }

        #endregion
    }
}





